package org.benjamin.lending.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
import org.benjamin.lending.domain.entity.LoginLog;
import org.benjamin.lending.domain.entity.MemberMoney;
import org.benjamin.lending.domain.entity.TrusteeshipRequest;
import org.benjamin.lending.domain.trusteeship.RequestParameter;
import org.benjamin.lending.service.MemberService;
import org.benjamin.lending.service.TrusteeshipService;
import org.benjamin.lending.utils.CommonsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "user")
public class UserController {

	@Autowired
	private TrusteeshipService trusteeshipService;

	@Autowired
	private MemberService memberService;

	@GetMapping(value = "index")
	public String index(ModelMap modelMap){
		String username = CommonsUtils.getSessionUser();
		MemberMoney memberMoney = this.memberService.findByMoneyByUsername(username);
		modelMap.addAttribute("memberMoney",memberMoney);
		return "user.index";
	}


	@PostMapping(value = "accountManager")
	public String accountManager(ModelMap modelMap, HttpServletRequest request){
		String username = CommonsUtils.getSessionUser();
		RequestParameter parameter = this.trusteeshipService.accountManager(username);
		modelMap.addAttribute("parameter",parameter);
		String requestType  = request.getHeader("X-Requested-With");
		if(StringUtils.isNotBlank(requestType)){
			return "request/ajaxSum";
		}
		return "request/sumpapay";
	}
	
	@RequestMapping(value = "safeset")
	public String safeset() {
		
		return "user.safeset";
	}
	
	@RequestMapping(value = "LoginLog")
	public String loginLog(ModelMap modelaMap) {
		String username = CommonsUtils.getSessionUser();
		Page<LoginLog> page = this.memberService.findLoginLog(username);
		modelaMap.addAttribute("page",page);
		return "user.loginLog";
	}
}
